/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class testChatterBot {

    static testMethod void myUnitTest() {
        //Use the PageReference Apex class to instantiate a page
            PageReference pageRef = Page.chatter;
            
            //In this case, the Visualforce page named 'chatter' is the starting point of this test method. 
            Test.setCurrentPage(pageRef);
            
            //Add parameters to page URL
            ApexPages.currentPage().getParameters().put('script', 'TestScript');
          
            // setup metadata for use by the test method
            User u = [Select Id from User where Profile.Name = 'System Administrator' Limit 1 ];
            u.Email_to_Salesforce_address__c = 'test@test.com' ;
            update u;
            
            // setup test parent record - in this case an Account
            Account a = new Account(Name = 'Test Account', Phone='03101011011');
            insert a;
            
            // setup test Script
            Chatter_Script__c  s = new Chatter_Script__c (  Name = 'TestScript', 
                                                            Focus_Actor__c = u.id );
            insert s;
            
            // setup test Text Post that loads 1 cycle in
            Chatter_Post__c p = new Chatter_Post__c ( Script__c = s.id, 
                                                      Name = 'Test Text Post', 
                                                      Actor__c = u.id,
                                                      Post_Parent_record_Id__c = a.id, 
                                                      Target_Cycle__c = 1, 
                                                      Post_type__c = 'Text');
            insert p;
            
            // setup test Comment that loads 1 cycle beyond parent post
            Chatter_Comment__c c = new Chatter_Comment__c( Name = 'Test Comment',
                                                           Post__c = p.id,
                                                           Actor__c = u.id,
                                                           Cycles_beyond_Post__c = 1);
            insert c;
            
            
            // setup test Post that loads when specific query criteria achieved
            Chatter_Post__c p2 = new Chatter_Post__c ( Script__c = s.id, 
                                                      Name = 'Test Text Post #2', 
                                                      Actor__c = u.id,
                                                      Post_Parent_record_Id__c = a.id, 
                                                      Target_Condition__c = 'Select Id from Account where Phone != null', 
                                                      Post_type__c = 'Text');
            insert p2;
            
            // setup test Post that is of type Content
            Chatter_Post__c p3 = new Chatter_Post__c ( Script__c = s.id, 
                                                      Name = 'Test Content Post', 
                                                      Actor__c = u.id,
                                                      Post_Parent_record_Id__c = a.id, 
                                                      Target_Cycle__c = 1, 
                                                      Post_type__c = 'Content');
            insert p3;
                // attach an attachment to p3 Post
                attachment attach = new attachment ( Name = 'Test Attachment', ParentId = p3.Id, Body = Blob.valueOf('aaaa') );
                insert attach;
                                                                
                                                      
            // setup test Comment that loads when post text field matches an existing post
            Chatter_Comment__c c2 = new Chatter_Comment__c( Name = 'Test Comment',
                                                           Post__c = p.id,
                                                           Actor__c = u.id,
                                                           Post_Text__c = 'Test Text Post',
                                                           Cycles_beyond_Post__c = 1);
            insert c2;
            
            
          
            
            
            //Instantiate and construct the controller class.   
            scriptController controller = new scriptController();
            controller.scriptName = s.Id;
            controller.increment = 1;
            
            // cycle the main method to fire events in test
            for (Integer i = 0; i<5 ; i++) controller.incrementCounter();
            
            // now update account record and retest for more coverage
            a.Phone = '03 93939333';
            update a;
            controller.incrementCounter();
            
            // now test reset methods
            controller.resetCounter();
            controller.reset();
            
            // test other methods
            //controller.getCount();
            //controller.getActions();
            //controller.getActionTable();
            
            
            
            
            
            
            // now test the inbound email handler
            
           // Create a new Text Post email
           Messaging.InboundEmail email = new Messaging.InboundEmail();
           Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
           email.subject = 'New Post';
           email.fromAddress = u.Email_to_Salesforce_address__c;
           email.plainTextBody = 'null<ActorId:'+u.Id+'><parentId:'+p.Post_Parent_record_Id__c+'><post.Post_type__c:Text><post.Link_URL__c:null><post.Name:Test Text Post #2><post.Id:'+p.Id+'><post.Cycle__c:null>';
           // call the class and test it with the data in the testMethod
           inboundEmailHandler emailServiceObj = new inboundEmailHandler();
           emailServiceObj.handleInboundEmail(email, env );
           
           // Create a new Link Post email
           Messaging.InboundEmail email2 = new Messaging.InboundEmail();
           Messaging.InboundEnvelope env2 = new Messaging.InboundEnvelope();
           email2.subject = 'New Post';
           email2.fromAddress = u.Email_to_Salesforce_address__c;
           email2.plainTextBody = 'null<ActorId:'+u.Id+'><parentId:'+p.Post_Parent_record_Id__c+'><post.Post_type__c:Link><post.Link_URL__c:http://test.com><post.Name:Test Text Post #2><post.Id:'+p.Id+'><post.Cycle__c:1>';
           // call the class and test it with the data in the testMethod
           inboundEmailHandler emailServiceObj2 = new inboundEmailHandler();
           emailServiceObj2.handleInboundEmail(email2, env2 );
           
           
           // Create a new Content Post email
           Messaging.InboundEmail email3 = new Messaging.InboundEmail();
           Messaging.InboundEnvelope env3 = new Messaging.InboundEnvelope();
           email3.subject = 'New Post';
           email3.fromAddress = u.Email_to_Salesforce_address__c;
           email3.plainTextBody = 'null<ActorId:'+u.Id+'><parentId:'+p.Post_Parent_record_Id__c+'><post.Post_type__c:Content><post.Link_URL__c:http://test.com><post.Name:Test Text Post #2><post.Id:'+p3.Id+'><post.Cycle__c:1>';
           // call the class and test it with the data in the testMethod
           inboundEmailHandler emailServiceObj3 = new inboundEmailHandler();
           emailServiceObj3.handleInboundEmail(email3, env3 );
           
            
           /*// set the body of the first attachment
           inAtt.body = blob.valueOf('test');
           inAtt.fileName = 'my attachment';
           inAtt.mimeTypeSubType = 'plain/txt';
        
           email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] {inAtt }; 
           
           // set the body of the second attachment
           inAtt2.body = blob.valueOf('test');
           inAtt2.fileName = 'my 2nd attachment';
           inAtt2.mimeTypeSubType = 'plain/txt';
        
           email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] {inAtt2 };
            */
            
            
    }
}